<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>VUE的性能优化 | 欢迎来到小羽的博客</title>
    <meta name="generator" content="VuePress 1.5.0">
    <link rel="icon" href="/favicon.ico">
    <script>
      var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?b61fe05f9bcd9cda05e839091f9a55a4";
        var s = document.getElementsByTagName("script")[0]; 
        s.parentNode.insertBefore(hm, s);
      })();
    </script>
    <script type="text/javascript" src="/push.js"></script>
    <script type="text/javascript" src="/bdseo.js"></script>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    <link rel="preload" href="/assets/css/0.styles.535c5086.css" as="style"><link rel="preload" href="/assets/js/app.14e3b6f2.js" as="script"><link rel="preload" href="/assets/js/3.38ef4e4c.js" as="script"><link rel="preload" href="/assets/js/1.dd4aade8.js" as="script"><link rel="preload" href="/assets/js/11.dbb0142d.js" as="script"><link rel="prefetch" href="/assets/js/10.7327de6d.js"><link rel="prefetch" href="/assets/js/12.b0df0a98.js"><link rel="prefetch" href="/assets/js/13.478ebb6f.js"><link rel="prefetch" href="/assets/js/14.5b94084a.js"><link rel="prefetch" href="/assets/js/15.159d1a32.js"><link rel="prefetch" href="/assets/js/16.cf37c681.js"><link rel="prefetch" href="/assets/js/17.61f89c90.js"><link rel="prefetch" href="/assets/js/18.52aae943.js"><link rel="prefetch" href="/assets/js/19.152fd37e.js"><link rel="prefetch" href="/assets/js/20.ce91f28e.js"><link rel="prefetch" href="/assets/js/21.47bc5b36.js"><link rel="prefetch" href="/assets/js/22.4bb2e24d.js"><link rel="prefetch" href="/assets/js/23.8156f657.js"><link rel="prefetch" href="/assets/js/24.867f6f46.js"><link rel="prefetch" href="/assets/js/25.1195288f.js"><link rel="prefetch" href="/assets/js/26.87eb9ee8.js"><link rel="prefetch" href="/assets/js/27.f67176ce.js"><link rel="prefetch" href="/assets/js/28.9574af6a.js"><link rel="prefetch" href="/assets/js/29.ce6e5d4c.js"><link rel="prefetch" href="/assets/js/30.5a16f9ff.js"><link rel="prefetch" href="/assets/js/31.d9981107.js"><link rel="prefetch" href="/assets/js/32.e0da40e8.js"><link rel="prefetch" href="/assets/js/33.f50cc0ef.js"><link rel="prefetch" href="/assets/js/34.6d343a0b.js"><link rel="prefetch" href="/assets/js/35.6e3a0c31.js"><link rel="prefetch" href="/assets/js/36.38b9af99.js"><link rel="prefetch" href="/assets/js/37.87aede0d.js"><link rel="prefetch" href="/assets/js/4.6cf77dfa.js"><link rel="prefetch" href="/assets/js/5.c81d3913.js"><link rel="prefetch" href="/assets/js/6.8fadf7af.js"><link rel="prefetch" href="/assets/js/7.bb84db9a.js"><link rel="prefetch" href="/assets/js/8.d9edba17.js"><link rel="prefetch" href="/assets/js/9.c228f55c.js">
    <link rel="stylesheet" href="/assets/css/0.styles.535c5086.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-dad8a512><div data-v-dad8a512><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-dad8a512 data-v-dad8a512><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-64685f0e data-v-dad8a512 data-v-dad8a512><h3 class="title" style="display:none;" data-v-64685f0e data-v-64685f0e>欢迎来到小羽的博客</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-64685f0e data-v-64685f0e><input type="password" value="" data-v-64685f0e> <span data-v-64685f0e>Konck! Knock!</span> <button data-v-64685f0e>OK</button></label> <div class="footer" style="display:none;" data-v-64685f0e data-v-64685f0e><span data-v-64685f0e><i class="iconfont reco-theme" data-v-64685f0e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-64685f0e>vuePress-theme-reco</a></span> <span data-v-64685f0e><i class="iconfont reco-copyright" data-v-64685f0e></i> <a data-v-64685f0e><span data-v-64685f0e>小羽</span>
            
          <!---->
          2020
        </a></span></div></div> <div class="hide" data-v-dad8a512><header class="navbar" data-v-dad8a512><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="欢迎来到小羽的博客" class="logo"> <span class="site-name">欢迎来到小羽的博客</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/安全/" class="nav-link"><i class="iconfont undefined"></i>
  安全
</a></li><li class="dropdown-item"><!----> <a href="/categories/网络/" class="nav-link"><i class="iconfont undefined"></i>
  网络
</a></li><li class="dropdown-item"><!----> <a href="/categories/运维/" class="nav-link"><i class="iconfont undefined"></i>
  运维
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      其他
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/lyff1006" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/xiaoyu-web" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/fly821760648" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-csdn"></i>
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://juejin.im/user/3597257778926973" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-juejin"></i>
  掘金
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://www.jianshu.com/u/4ab50cbafc3f" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-jianshu"></i>
  简书
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-dad8a512></div> <aside class="sidebar" data-v-dad8a512><div class="personal-info-wrapper" data-v-ca798c94 data-v-dad8a512><img src="/logo.png" alt="author-avatar" class="personal-img" data-v-ca798c94> <h3 class="name" data-v-ca798c94>
    小羽
  </h3> <div class="num" data-v-ca798c94><div data-v-ca798c94><h3 data-v-ca798c94>22</h3> <h6 data-v-ca798c94>文章</h6></div> <div data-v-ca798c94><h3 data-v-ca798c94>35</h3> <h6 data-v-ca798c94>标签</h6></div></div> <hr data-v-ca798c94></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/安全/" class="nav-link"><i class="iconfont undefined"></i>
  安全
</a></li><li class="dropdown-item"><!----> <a href="/categories/网络/" class="nav-link"><i class="iconfont undefined"></i>
  网络
</a></li><li class="dropdown-item"><!----> <a href="/categories/运维/" class="nav-link"><i class="iconfont undefined"></i>
  运维
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      其他
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/lyff1006" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/xiaoyu-web" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://blog.csdn.net/fly821760648" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-csdn"></i>
  CSDN
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://juejin.im/user/3597257778926973" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-juejin"></i>
  掘金
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://www.jianshu.com/u/4ab50cbafc3f" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-jianshu"></i>
  简书
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-64685f0e data-v-dad8a512><h3 class="title" style="display:none;" data-v-64685f0e data-v-64685f0e>VUE的性能优化</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-64685f0e data-v-64685f0e><input type="password" value="" data-v-64685f0e> <span data-v-64685f0e>Konck! Knock!</span> <button data-v-64685f0e>OK</button></label> <div class="footer" style="display:none;" data-v-64685f0e data-v-64685f0e><span data-v-64685f0e><i class="iconfont reco-theme" data-v-64685f0e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-64685f0e>vuePress-theme-reco</a></span> <span data-v-64685f0e><i class="iconfont reco-copyright" data-v-64685f0e></i> <a data-v-64685f0e><span data-v-64685f0e>小羽</span>
            
          <!---->
          2020
        </a></span></div></div> <div data-v-dad8a512><main class="page"><div class="page-title" style="display:none;"><h1>VUE的性能优化</h1> <hr> <div data-v-aa64b9d0><i class="iconfont reco-account" data-v-aa64b9d0><span data-v-aa64b9d0>小羽</span></i> <i class="iconfont reco-date" data-v-aa64b9d0><span data-v-aa64b9d0>2020-07-29</span></i> <!----> <i class="iconfont reco-tag tags" data-v-aa64b9d0><span class="tag-item" data-v-aa64b9d0>vue</span><span class="tag-item" data-v-aa64b9d0>性能优化</span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><h2 id="_1-源码优化"><a href="#_1-源码优化" class="header-anchor">#</a> 1.源码优化</h2> <h3 id="_1-1-v-for使用key"><a href="#_1-1-v-for使用key" class="header-anchor">#</a> 1.1 v-for使用key</h3> <p>key 可以唯一标识一个循环个体，可以使用例如 item.id 作为 key，假如数组数据是这样的 ['a' , 'b', 'c', 'a'],使用 :key=&quot;item&quot; 显然没有意义，更好的办法就是在循环的时候 (item, index) in arr，然后 :key=&quot;index&quot;来确保key 的唯一性。不设置key值时vue会抛出警告。</p> <h4 id="_1-1-1-diff算法"><a href="#_1-1-1-diff算法" class="header-anchor">#</a> 1.1.1 diff算法</h4> <p>对于渲染watcher触发时会执行<code>vm._update(vm._render(), hydrating)</code>，在<code>vm._undata</code>方法中会调用<code>vm.__patch__</code>，而<code>vm.__patch__</code>指向patch方法，diff计算是指在调用patch方法开始，用sameVnode方法判断节点是否值得比较，若不值得直接新节点替换旧节点后结束。值得对比进入patchVnode方法，分别处理一下几种情况，若新旧节点都有文本节点，新节点下的文本节点直接替换旧节点下的文本节点，如果新节点有子节点，旧节点没有子节点，那么直接把新节点查到旧节点的父级中，如果新节点没有子节点，旧节点有子节点，那么旧节点的父级下的子节点都删了。如果新旧节点都有子节点，进入updateChildren方法，通过旧头新头、旧尾新尾、旧头新尾、旧尾新头四次交叉比对，如果值得对比再进入patchVnode方法，如果都不值得对比，有key用map获得值得对比的旧节点，没有key通过循环旧节点获得值得对比的旧节点。当新节点都对比完，旧节点还没对比完，将还没对比完的旧节点删掉。当旧节点都对比完，新节点还没对比完，将新节点添加到最后一个对比过的新节点后面，完成diff计算。</p> <p>diff计算就是对比新旧虚拟DOM（virtual DOM），virtual DOM是将真实的DOM的数据抽取出来，以对象的形式模拟树形结构，再说简白一点，diff计算就是对两个对象进行对比。</p> <p>在vue中，把dom-diff过程叫做patch过程。patch的意思是补丁，即对旧的vnode进行修补，打补丁从而得到新的vnode。整个patch的过程无非就做了三件事情。创建节点、删除节点、更新节点。</p> <ul><li>创建节点：新的vnode中有而旧的vnode中没有，就在旧的vnode中创建节点。</li> <li>删除节点：新的vnode中没有而旧的vnode中有，就在旧的vnode中删除节点。</li> <li>更新节点：新的vnode和旧的vnode中都有，就以新得vnode为准，更新旧的vnode。</li></ul> <p><strong>总的来说就是，已新的vnode为基准，改造旧的vnode使之成为新的vnode一样，这就是patch过程需要做的事情。此外，在采取diff算法比较新旧节点的时候，比较只会在同层级进行, 不会跨层级比较。</strong></p> <p><img src="/assets/img/image-20200807174602393.062262cd.png" alt="image-20200807174602393"></p> <p>diff算法的每次对比逻辑大概如下所示</p> <ul><li>1、在patch方法内，用sameVnode判断新旧节点是否值得比较。</li> <li>2、如果不值得比较，直接在旧节点的父级中添加新节点，然后删除旧节点，退出对比。</li> <li>3、如果值得比较，调用patchVnode方法。</li> <li>4、如果新旧节点是否完全相等，如果是，退出对比。</li> <li>5、如果不是，找到对应的真实DOM，记为el。</li> <li>6、如果新旧节点都有文本节点并且不相等，那么将el的文本节点设置为新节点的文本节点，退出对比。</li> <li>7、如果新节点有子节点，旧节点没有子节点，则将新节点的子节点生成真实DOM后添加到el中，退出对比。</li> <li>8、如果新节点没有子节点，旧节点有子节点，则删除el的子节点，退出对比。</li> <li>9、如果新节点和旧节点都有子节点，则开始对比它们的子节点，用的是updateChildren方法。</li> <li>10、将旧节点的子节点记为<code>oldCh</code>是个数组,其头部用<code>oldCh[oldStartIdx]</code>获取记为<code>oldStartVnode</code>，<code>oldStartIdx</code>初始为0。其尾部用<code>oldCh[oldEndIdx]</code>获取记为<code>oldEndVnode</code>，<code>oldEndIdx</code>初始为<code>oldCh.length - 1</code>。</li> <li>11、将旧节点的子节点记为<code>newCh</code>是个数组,其头部用<code>newCh[newStartIdx]</code>获取记为<code>newStartVnode</code>，<code>newStartIdx</code>初始为0。其尾部用<code>newCh[newEndIdx]</code>获取记为<code>newEndVnode</code>，<code>newEndIdx</code>初始为<code>newCh.length - 1</code>。</li> <li>12、将旧子节点的头部和新子节点的头部，简称旧头和新头用sameVnode判断是否值得比较。</li> <li>13、如果值得比较，调用patchVnode方法，重新执行第3步。同时用<code>oldCh[++oldStartIdx]</code>重新获取旧子节点头部，用<code>newCh[++newStartIdx]</code>重新获取新子节点头部。</li> <li>14、如果不值得比较，将旧子节点的尾部和新子节点的尾部，简称旧尾和新尾用sameVnode判断是否值得比较。</li> <li>15、如果值得比较，调用patchVnode方法，重新执行第3步。同时用<code>oldCh[--oldEndIdx]</code>重新获取旧子节点尾部，重新用<code>newCh[--newEndIdx]</code>获取新子节点尾部。</li> <li>16、如果不值得比较，将旧子节点的头部和新子节点的尾部，简称旧头和新尾用sameVnode判断是否值得比较。</li> <li>17、如果值得比较，调用patchVnode方法，重新执行第3步。同时将旧子节点的头部<code>oldStartVnode</code>对应的真实DOM移动到旧子节点的尾部<code>oldEndVnode</code>对应的真实DOM后面。同时用<code>oldCh[++oldStartIdx]</code>重新获取旧子节点头部，用<code>newCh[--newEndIdx]</code>重新获取新子节点尾部。</li> <li>18、如果不值得比较，将旧子节点的尾部和新子节点的头部，简称旧尾和新头用sameVnode判断是否值得比较。</li> <li>19、如果值得比较，调用patchVnode方法，重新执行第3步。同时将旧子节点的尾部<code>oldEndVnode</code>对应的真实DOM移动到旧子节点的头部<code>oldStartVnode</code>对应的真实DOM后面。同时用<code>oldCh[--oldEndIdx]</code>重新获取旧子节点尾部，用<code>newCh[++newStartIdx]</code>重新获取新子节点头部。</li> <li>20、如果不值得比较，如果旧子节点有key，可以用createKeyToOldIdx方法获得以旧子节点的key为健，其下标为值的map结构,记为<code>oldKeyToIdx</code>。</li> <li>21、如果新子节点的头部<code>newStartVnode</code>有key属性，直接通过<code>oldKeyToIdx[newStartVnode.key]</code>获取对应的下标<code>idxInOld</code>。</li> <li>22、如果新子节点的头部<code>newStartVnode</code>没有key属性，要用过findIdxInOld方法，找到值得对比的旧子节点对应的下标<code>idxInOld</code>。</li> <li>23、经过查找。如果<code>idxInOld</code>不存在。则调用createElm方法直接生成<code>newStartVnode</code>对应的真实DOM插入<code>oldStartVnode</code>对应真实DOM前面。</li> <li>24、如果<code>idxInOld</code>存在，则把用通过<code>oldCh[idxInOld]</code>获取到Vnode记为<code>vnodeToMove</code>和<code>newStartVnode</code>用sameVnode判断是否值得比较。</li> <li>25、如果值得比较，调用patchVnode方法，重新执行第3步。同时执行<code>oldCh[idxInOld] = undefined</code>，免得被重复比较。同时将<code>vnodeToMove</code>对应的真实DOM移动到旧子节点的头部<code>oldStartVnode</code>对应的真实DOM前面。</li> <li>26、如果不值得比较，则调用createElm方法直接生成<code>newStartVnode</code>对应的真实DOM插入<code>oldStartVnode</code>对应真实DOM前面。</li> <li>27、用<code>newCh[++newStartIdx]</code>重新获取新子节点头部</li> <li>28、如果满足<code>oldStartIdx &lt;= oldEndIdx &amp;&amp; newStartIdx &lt;= newEndIdx</code>继续执行步骤9。</li> <li>29、如果<code>oldStartIdx &gt; oldEndIdx</code>，说明所有旧子节点已经都比对完了，还剩下未比对的新子节点都调用createElm方法生成对应的真实DOM，插到<code>newCh[newEndIdx + 1]</code>对应的真实DOM后面。</li> <li>30、如果<code>newStartIdx &gt; newEndIdx</code>，说明所有新子节点都比较完，那么还剩下的旧子节点都删除掉。</li></ul> <h4 id="_1-1-2-为什么加key会提高diff计算速度"><a href="#_1-1-2-为什么加key会提高diff计算速度" class="header-anchor">#</a> 1.1.2  为什么加key会提高diff计算速度</h4> <p>经过旧头新头、旧尾新尾、旧头新尾、旧尾新头四次交叉比对后，都没有匹配到值得比对的节点，这时如果新节点有key的话。可以通过map直接获得值得对比的旧节点的下标，如果没有key的话，就要通过循环旧节点数组用sameVnode方法判断新节点和该旧节点是否值得比较，值得就返回该旧节点的下标。显然通过map比通过循环数组的计算速度来的快。</p> <h3 id="_1-2-路由懒加载"><a href="#_1-2-路由懒加载" class="header-anchor">#</a> 1.2 路由懒加载</h3> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> routes <span class="token operator">=</span> <span class="token punctuation">[</span>
    <span class="token punctuation">{</span>
        path<span class="token operator">:</span><span class="token string">'/login'</span><span class="token punctuation">,</span>
        name<span class="token operator">:</span><span class="token string">'login'</span><span class="token punctuation">,</span>
        <span class="token function-variable function">component</span><span class="token operator">:</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span><span class="token keyword">import</span><span class="token punctuation">(</span><span class="token string">'@amazon/views/login.vue'</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
 <span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="_1-3-及时销毁定时器"><a href="#_1-3-及时销毁定时器" class="header-anchor">#</a> 1.3 及时销毁定时器</h3> <p>离开路由时需要及时销毁定时器</p> <h3 id="_1-4-合理利用生命周期"><a href="#_1-4-合理利用生命周期" class="header-anchor">#</a> 1.4 合理利用生命周期</h3> <blockquote><div class="language- line-numbers-mode"><pre class="language-text"><code>我们请求回来的初始化数据或者基础数据是不是需要挂在Vue的Data上面？（是的，需要√）
--beforeCreate的时候Data有没有生成？（答案是：没有。×）
--所以这一步是无法把数据挂到Vue的Data上面的
然后我继续看created的时候Data有没有生成？（答案是：生成啦。√）
--所以这一步我们是可以把数据挂到Vue的Data上面的,是不是可以放在这里啦？
--是的，可以放在这里啦。所以最后结论就是可以放在created里面。
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></blockquote> <blockquote><p>补充分析：</p> <p>（1）如果把所有请求放在created里面的话,请求过多会,加载太慢会导致页面出现短暂的白屏情况,一般上我写的话,接口不复杂会放created里面,接口多复杂的话会放在mounted里面.</p> <p>（2）created 是加载DOM,html之后 就马上执行, 比如初始化,获取屏幕高度调整,赋值等等,而mounted就是执行包括js之后,准备开始调用方法,也就是说 类似传统开发那样,先加载jquery 再调用插件</p></blockquote> <h3 id="_1-5-利用v-if和v-show减少初始化渲染和切换渲染的性能开销"><a href="#_1-5-利用v-if和v-show减少初始化渲染和切换渲染的性能开销" class="header-anchor">#</a> 1.5 利用v-if和v-show减少初始化渲染和切换渲染的性能开销</h3> <p>在页面加载时，利用v-if来控制组件仅在首次使用时渲染减少初始化渲染，随后用v-show来控制组件显示隐藏减少切换渲染的性能开销。</p> <h3 id="_1-6-computed、watch、methods区分使用场景"><a href="#_1-6-computed、watch、methods区分使用场景" class="header-anchor">#</a> 1.6 computed、watch、methods区分使用场景</h3> <p>对于有些需求，computed、watch、methods都可以实现，但是还是要区分一下使用场景。用错场景虽然功能实现了但是影响了性能。</p> <ul><li>computed：
<ul><li>一个数据受多个数据影响的。</li> <li>该数据要经过性能开销比较大的计算，如它需要遍历一个巨大的数组并做大量的计算才能得到，这时就可以利用<code>computed</code>的缓存特性，只有它计算时依赖的数据发现变化时才会重新计算，否则直接返回缓存值。</li></ul></li> <li>watch：
<ul><li>一个数据影响多个数据的。</li> <li>当数据变化时，需要执行异步或开销较大的操作时。如果数据变化时请求一个接口。</li></ul></li> <li>methods：
<ul><li>希望数据是实时更新，不需要缓存。</li></ul></li></ul> <p>1.7 提前处理好数据解决v-if和v-for必须同级的问题</p> <p>因为当Vue处理指令时，<code>v-for</code>比<code>v-if</code>具有更高的优先级，意味着<code>v-if</code> 将分别重复运行于每个<code>v-for</code>循环中。</p> <p>可以在computed中提前把要<code>v-for</code>的数据中<code>v-if</code>的数据项给过滤处理了。</p> <div class="language-vue line-numbers-mode"><pre class="language-vue"><code>//userList.vue
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">v-for</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>item in userList<span class="token punctuation">&quot;</span></span> <span class="token attr-name">:key</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>item.id<span class="token punctuation">&quot;</span></span> <span class="token attr-name">v-if</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>item.age &gt; 18<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>{{ item.name }}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><div class="language-vue line-numbers-mode"><pre class="language-vue"><code>//userList.vue
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">v-for</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>item in userComputedList<span class="token punctuation">&quot;</span></span> <span class="token attr-name">:key</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>item.id<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>{{ item.name }}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>
export default {
    computed:{
        userComputedList:function(){
            return this.userList.filter(function (item) {
                return item.age &gt; 18
            })
        }
    }
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h3 id="_1-7-利用v-once处理只会渲染一次的元素或组件"><a href="#_1-7-利用v-once处理只会渲染一次的元素或组件" class="header-anchor">#</a> 1.7 利用v-once处理只会渲染一次的元素或组件</h3> <p>只渲染元素和组件一次。随后的重新渲染，元素/组件及其所有的子节点将被视为静态内容并跳过。这可以用于优化更新性能。</p> <p>例如某个页面是合同范文，里面大部分内容从服务端获取且是固定不变，只有姓名、产品、金额等内容会变动。这时就可以把<code>v-once</code>添加到那些包裹固定内容的元素上，当生成新的合同可以跳过那些固定内容，只重新渲染姓名、产品、金额等内容即可。</p> <p>和<code>v-if</code>一起使用时，<code>v-once</code>不生效。在<code>v-for</code>循环内的元素或组件上使用，必须加上key。</p> <h3 id="_1-8-长列表性能优化（object-freeze）"><a href="#_1-8-长列表性能优化（object-freeze）" class="header-anchor">#</a> 1.8 长列表性能优化（Object.freeze）</h3> <p>Vue初始化过程中，会把data传入observe函数中进行数据劫持，把data中的数据都转换成响应式的。</p> <p>因此在项目中如果遇到不需要响应式变化的数据，可以用<code>Object.freeze()</code>把该数据冻结了，可以跳过初始化时数据劫持的步骤，大大提高初次渲染速度。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
  <span class="token function-variable function">data</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">(</span><span class="token punctuation">{</span>
    users<span class="token operator">:</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  <span class="token keyword">async</span> <span class="token function">created</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> users <span class="token operator">=</span> <span class="token keyword">await</span> axios<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">&quot;/api/users&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>users <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">freeze</span><span class="token punctuation">(</span>users<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h3 id="_1-9-提前过滤掉非必须数据，优化data选项中的数据结构"><a href="#_1-9-提前过滤掉非必须数据，优化data选项中的数据结构" class="header-anchor">#</a> 1.9 提前过滤掉非必须数据，优化data选项中的数据结构</h3> <p>接收服务端传来的数据，都会有一些渲染页面时用不到的数据。服务端的惯例，宁可多传也不会少传。</p> <p>所以要先把服务端传来的数据中那些渲染页面用不到的数据先过滤掉。然后再赋值到data选项中。可以避免去劫持那些非渲染页面需要的数据，减少循环和递归调用，从而提高渲染速度。</p> <h3 id="_1-10-避免在v-for循环中读取data中数组类型的数据"><a href="#_1-10-避免在v-for循环中读取data中数组类型的数据" class="header-anchor">#</a> 1.10 避免在v-for循环中读取data中数组类型的数据</h3> <p>为什么要避免在v-for循环中读取data中数组类型的数据，因为在数据劫持中会调用defineReactive函数中。由于 getter是函数，并且引用了 <code>dep</code>、<code>childOb</code>，形成了闭包，所以 <code>dep</code>、<code>childOb</code> 一直存在于内存（每个数据的getter函数）中，<code>dep</code>是每个数据的依赖收集容器，<code>childOb</code>是经过响应式处理后的数据。</p> <p>在渲染视图、使用watch监听、使用计算属性过程中，读取数据，都会对<code>Dep.target</code>进行赋值，其值为Watcher（依赖），例如在渲染视图过程中读取数据时，<code>Dep.target</code>为renderWatcher。</p> <p>接着先调用<code>dep.depend()</code>给自身收集依赖，如果val（自身的值）不是对象，则<code>childOb</code>为false。如果val（自身的值）是对象，用<code>childOb.dep.depend()</code>收集依赖，若value（自身的值）是数组用<code>dependArray(value)</code>递归每一项来收集依赖。</p> <p>为什么要避免在v-for循环中读取data中数组类型的数据，其原因就是<strong>若value（自身的值）是数组用<code>dependArray(value)</code>递归每一项来收集依赖</strong></p> <p>举个简单的栗子，表格中每行有两个输入框，分别可以输入驾驶员和电话，代码这么实现。</p> <div class="language-vue line-numbers-mode"><pre class="language-vue"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>g-table-content<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>el-table</span> <span class="token attr-name">:data</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>tableData<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>el-table-column</span> <span class="token attr-name">prop</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>carno<span class="token punctuation">&quot;</span></span> <span class="token attr-name">label</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>车牌号<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>el-table-column</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>el-table-column</span> <span class="token attr-name">prop</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>cartype<span class="token punctuation">&quot;</span></span> <span class="token attr-name">label</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>车型<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>el-table-column</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>el-table-column</span> <span class="token attr-name">label</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>驾驶员<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span> <span class="token attr-name">slot-scope</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>{row,column,$index}<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
                    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>el-input</span> <span class="token attr-name">v-model</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>driverList[$index].name<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>el-input</span><span class="token punctuation">&gt;</span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>el-table-column</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>el-table-column</span> <span class="token attr-name">label</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>电话<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span> <span class="token attr-name">slot-scope</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>{row,column,$index}<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
                    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>el-input</span> <span class="token attr-name">v-model</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>driverList[$index].phone<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>el-input</span><span class="token punctuation">&gt;</span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>el-table-column</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>el-table</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><p>假设表格有500条数据，那么读取driverList共500次，每次都读取driverList都会进入<code>dependArray(value)</code>中，总共要循环500*500=25万次，若有分页，每次切换页码，都会至少循环25万次。</p> <p>如果我们在从服务获取到数据后，做了如下预处理，在赋值给<code>this.tableData</code>，会是怎么样？</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>res<span class="token punctuation">.</span>data<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token parameter">item</span> <span class="token operator">=&gt;</span><span class="token punctuation">{</span>
    item<span class="token punctuation">.</span>name<span class="token operator">=</span><span class="token string">''</span><span class="token punctuation">;</span>
    item<span class="token punctuation">.</span>phone<span class="token operator">=</span><span class="token string">''</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><div class="language-vue line-numbers-mode"><pre class="language-vue"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>g-table-content<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>el-table</span> <span class="token attr-name">:data</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>tableData<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>el-table-column</span> <span class="token attr-name">prop</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>carno<span class="token punctuation">&quot;</span></span> <span class="token attr-name">label</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>车牌号<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>el-table-column</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>el-table-column</span> <span class="token attr-name">prop</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>cartype<span class="token punctuation">&quot;</span></span> <span class="token attr-name">label</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>车型<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>el-table-column</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>el-table-column</span> <span class="token attr-name">label</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>驾驶员<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span> <span class="token attr-name">slot-scope</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>{row}<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
                    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>el-input</span> <span class="token attr-name">v-model</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>row.name<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>el-input</span><span class="token punctuation">&gt;</span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>el-table-column</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>el-table-column</span> <span class="token attr-name">label</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>电话<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span> <span class="token attr-name">slot-scope</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>{row}<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
                    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>el-input</span> <span class="token attr-name">v-model</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>row.phone<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>el-input</span><span class="token punctuation">&gt;</span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>el-table-column</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>el-table</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><p>也可以实现需求，渲染过程中求值时也不会进入<code>dependArray(value)</code>中,也不会造成25万次的不必要的循环。大大提高了性能。</p> <h3 id="_1-11-防抖和节流"><a href="#_1-11-防抖和节流" class="header-anchor">#</a> 1.11 防抖和节流</h3> <p>防抖和节流是针对用户操作的优化。</p> <h4 id="_1-11-1-防抖"><a href="#_1-11-1-防抖" class="header-anchor">#</a> 1.11.1 防抖</h4> <p>触发事件后规定时间内事件只会执行一次。简单来说就是防止手抖，短时间操作了好多次。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">Debounce</span><span class="token punctuation">(</span><span class="token parameter">fn<span class="token punctuation">,</span> t</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">let</span> delay <span class="token operator">=</span> t <span class="token operator">||</span> <span class="token number">500</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> timer<span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> args <span class="token operator">=</span> arguments<span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>timer<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">clearTimeout</span><span class="token punctuation">(</span>timer<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        timer <span class="token operator">=</span> <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            timer <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
            <span class="token function">fn</span><span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span> delay<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h4 id="_1-11-2-节流"><a href="#_1-11-2-节流" class="header-anchor">#</a> 1.11.2 节流</h4> <p>事件在规定时间内只执行一次。</p> <div class="language-JavaScript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">Throttle</span><span class="token punctuation">(</span><span class="token parameter">fn<span class="token punctuation">,</span> t</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">let</span> last<span class="token punctuation">;</span>
    <span class="token keyword">let</span> timer<span class="token punctuation">;</span>
    <span class="token keyword">let</span> interval <span class="token operator">=</span> t <span class="token operator">||</span> <span class="token number">500</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> args <span class="token operator">=</span> arguments<span class="token punctuation">;</span>
        <span class="token keyword">let</span> now <span class="token operator">=</span> <span class="token operator">+</span><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>last <span class="token operator">&amp;&amp;</span> now <span class="token operator">-</span> last <span class="token operator">&lt;</span> interval<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">clearTimeout</span><span class="token punctuation">(</span>timer<span class="token punctuation">)</span><span class="token punctuation">;</span>
            timer <span class="token operator">=</span> <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
                last <span class="token operator">=</span> now<span class="token punctuation">;</span>
                <span class="token function">fn</span><span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span> interval<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            last <span class="token operator">=</span> now<span class="token punctuation">;</span>
            <span class="token function">fn</span><span class="token punctuation">.</span><span class="token function">apply</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><h4 id="_1-11-3-应用场景"><a href="#_1-11-3-应用场景" class="header-anchor">#</a> 1.11.3 应用场景</h4> <p>节流不管事件有没有触发还是频繁触发，在规定时间内一定会只执行一次事件，而防抖是在规定时间内事件被触发，且是最后一次被触发才执行一次事件。假如事件需要定时执行，但是其他操作也会让事件执行，这种场景可以用节流。假如事件不需要定时执行，需被触发才执行，且短时间内不能执行多次，这种场景可以用防抖。</p> <h3 id="_1-12-图片懒加载"><a href="#_1-12-图片懒加载" class="header-anchor">#</a> 1.12 图片懒加载</h3> <p>关于图片懒加载，可以用vue-lazyload插件实现。</p> <p>执行命令<code>npm install vue-lazyload --save</code>安装vue-lazyload插件。在main.js中引入配置</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">import</span> VueLazyload <span class="token keyword">from</span> <span class="token string">'vue-lazyload'</span><span class="token punctuation">;</span>
Vue<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span>VueLazyload<span class="token punctuation">,</span> <span class="token punctuation">{</span>
  preLoad<span class="token operator">:</span> <span class="token number">1.3</span><span class="token punctuation">,</span><span class="token comment">//预载高度比例</span>
  error<span class="token operator">:</span> <span class="token string">'dist/error.png'</span><span class="token punctuation">,</span><span class="token comment">//加载失败显示图片</span>
  loading<span class="token operator">:</span> <span class="token string">'dist/loading.gif'</span><span class="token punctuation">,</span><span class="token comment">//加载过程中显示图片</span>
  attempt<span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span><span class="token comment">//尝试次数</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>在项目中使用</p> <div class="language-vue line-numbers-mode"><pre class="language-vue"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>img</span> <span class="token attr-name">v-lazy</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>/static/img/1.png<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_1-13-组件库的按需引入"><a href="#_1-13-组件库的按需引入" class="header-anchor">#</a> 1.13 组件库的按需引入</h3> <p>组件库按需引入的方法，一般文档都会介绍。</p> <h3 id="_1-14-dns预解析"><a href="#_1-14-dns预解析" class="header-anchor">#</a> 1.14 DNS预解析</h3> <p>域名解析：从域名查询IP的过程，这个过程一般都很快的，但也会引起延迟。一般浏览器会适当的对解析结果缓存，并对页面中出现的新域名进行预解析，但并不是所有的浏览器都会这么做，为了帮助其它浏览器对某些域名进行预解析，你可以在页面的html标签中添加dns-prefetch告诉浏览器对指定域名预解析，如下：</p> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>link</span> <span class="token attr-name">rel</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>dns-prefetch<span class="token punctuation">&quot;</span></span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>//domain.com<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_1-15-无限列表的优化"><a href="#_1-15-无限列表的优化" class="header-anchor">#</a> 1.15 无限列表的优化</h3> <p>如果你的应用存在非常长或者无限滚动的列表，那么需要采用 窗口化 的技术来优化性能，只需要渲染少部分区域的内容，减少重新渲染组件和创建 dom 节点的时间。 你可以参考以下开源项目 <a href="https://github.com/tangbc/vue-virtual-scroll-list" target="_blank" rel="noopener noreferrer">vue-virtual-scroll-list<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a> 和 <a href="https://github.com/Akryum/vue-virtual-scroller" target="_blank" rel="noopener noreferrer">vue-virtual-scroller<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>  来优化这种无限列表的场景的。</p> <h3 id="_1-16-服务端渲染-ssr-or-预渲染"><a href="#_1-16-服务端渲染-ssr-or-预渲染" class="header-anchor">#</a> 1.16 服务端渲染 SSR or 预渲染</h3> <p>服务端渲染是指 Vue 在客户端将标签渲染成的整个 html 片段的工作在服务端完成，服务端形成的 html 片段直接返回给客户端这个过程就叫做服务端渲染。</p> <p><strong>（1）服务端渲染的优点：</strong></p> <ul><li>更好的 SEO： 因为 SPA 页面的内容是通过 Ajax 获取，而搜索引擎爬取工具并不会等待 Ajax 异步完成后再抓取页面内容，所以在 SPA 中是抓取不到页面通过 Ajax 获取到的内容；而 SSR 是直接由服务端返回已经渲染好的页面（数据已经包含在页面中），所以搜索引擎爬取工具可以抓取渲染好的页面；</li> <li>更快的内容到达时间（首屏加载更快）： SPA 会等待所有 Vue 编译后的 js 文件都下载完成后，才开始进行页面的渲染，文件下载等需要一定的时间等，所以首屏渲染需要一定的时间；SSR 直接由服务端渲染好页面直接返回显示，无需等待下载 js 文件及再去渲染等，所以 SSR 有更快的内容到达时间；</li></ul> <p><strong>（2）服务端渲染的缺点：</strong></p> <ul><li>更多的开发条件限制： 例如服务端渲染只支持 beforCreate 和 created 两个钩子函数，这会导致一些外部扩展库需要特殊处理，才能在服务端渲染应用程序中运行；并且与可以部署在任何静态文件服务器上的完全静态单页面应用程序 SPA 不同，服务端渲染应用程序，需要处于 Node.js server 运行环境；</li> <li>更多的服务器负载：在 Node.js 中渲染完整的应用程序，显然会比仅仅提供静态文件的 server 更加大量占用CPU 资源，因此如果你预料在高流量环境下使用，请准备相应的服务器负载，并明智地采用缓存策略。</li></ul> <p>如果你的项目的 SEO 和 首屏渲染是评价项目的关键指标，那么你的项目就需要服务端渲染来帮助你实现最佳的初始加载性能和 SEO。如果你的 Vue 项目只需改善少数营销页面（例如  <code>/， /about， /contac</code>t 等）的 SEO，那么你可能需要<strong>预渲染</strong>，在构建时 (build time) 简单地生成针对特定路由的静态 HTML 文件。优点是设置预渲染更简单，并可以将你的前端作为一个完全静态的站点，具体你可以使用 <a href="https://github.com/chrisvfritz/prerender-spa-plugin" target="_blank" rel="noopener noreferrer">prerender-spa-plugin<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a> 就可以轻松地添加预渲染 。</p> <h2 id="_2-打包优化"><a href="#_2-打包优化" class="header-anchor">#</a> 2.打包优化</h2> <h3 id="_2-1-生产环境不生成-map文件"><a href="#_2-1-生产环境不生成-map文件" class="header-anchor">#</a> 2.1 生产环境不生成.map文件</h3> <p>npm run build编译之后，我们查看编译生成的文件，发现有很多.map文件，这些文件占了不小的空间。.map文件的作用是帮助编译后的代码调试，但是我们上线的代码已经调试完成，所以上线时可以不生成.map文件。</p> <p>在vue.config.js文件中添加productionSourceMap: false</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
  productionSourceMap<span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="_2-2-vue开启gzip压缩"><a href="#_2-2-vue开启gzip压缩" class="header-anchor">#</a> 2.2 vue开启gzip压缩</h3> <p>vue中开启gzip压缩必须要配合服务器的静态gzip模块使用，否则不会生效。</p> <p>如nginx服务器则需要配合ngx_http_gzip_static_module模块进行使用</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
 <span class="token function-variable function">configureWebpack</span><span class="token operator">:</span><span class="token parameter">config</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
 	<span class="token keyword">if</span><span class="token punctuation">(</span>process<span class="token punctuation">.</span>env<span class="token punctuation">.</span><span class="token constant">NODE_ENV</span> <span class="token operator">===</span> <span class="token string">'production'</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
 		<span class="token keyword">return</span> <span class="token punctuation">{</span>
 			plugins<span class="token operator">:</span><span class="token punctuation">[</span><span class="token keyword">new</span> <span class="token class-name">CompressionPlugin</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
 				test<span class="token operator">:</span><span class="token regex">/\.js$|\.html$\.css/</span><span class="token punctuation">,</span>
 				threshold<span class="token operator">:</span><span class="token number">1024</span><span class="token punctuation">,</span><span class="token comment">//对超过1k的文件进行压缩</span>
 				deleteOriginalAssets<span class="token operator">:</span><span class="token boolean">false</span><span class="token comment">//是否删除源文件</span>
 			<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">]</span>
 		<span class="token punctuation">}</span>
 	<span class="token punctuation">}</span>
 <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><h3 id="_2-3-cdn优化"><a href="#_2-3-cdn优化" class="header-anchor">#</a> 2.3 cdn优化</h3> <p>CDN 的全称是 Content Delivery Network，即 内容分发网络。</p> <p>工作原理：将源码的资源缓存到位于全国各地的 CDN 节点上，用户请求资源时，就近返回节点上缓存的资源，而不需要每个用户的请求都回源站点获取，避免网络拥塞、分担源站压力，保证用户访问资源的速度和体验。</p> <p>cdn的优点：</p> <ul><li>为了加速网站的访问。</li> <li>为了实现跨运营商、跨地域的全网覆盖</li> <li>为了保障网站安全</li> <li>为了异地备源</li> <li>为了节约成本投入</li> <li>为了更专注业务本身</li></ul> <p>在Webpack中的<code>externals</code>配置选项，可避免将第三方依赖打包，而是在项目运行时从外部获取第三方依赖。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">//vue.config.js</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
  <span class="token function-variable function">chainWebpack</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">config</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    <span class="token comment">//配置cdn</span>
    config<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string">&quot;externals&quot;</span><span class="token punctuation">,</span><span class="token punctuation">{</span>
      <span class="token string">'vue'</span><span class="token operator">:</span> <span class="token string">'Vue'</span><span class="token punctuation">,</span>
      <span class="token string">'vue-router'</span><span class="token operator">:</span> <span class="token string">'VueRouter'</span><span class="token punctuation">,</span>
      <span class="token string">'vuex'</span><span class="token operator">:</span> <span class="token string">'Vuex'</span><span class="token punctuation">,</span>
      <span class="token string">'axios'</span><span class="token operator">:</span> <span class="token string">'axios'</span><span class="token punctuation">,</span>
      <span class="token string">'echarts'</span><span class="token operator">:</span> <span class="token string">&quot;echarts&quot;</span><span class="token punctuation">,</span>
      <span class="token string">&quot;view-design&quot;</span><span class="token operator">:</span><span class="token string">&quot;iview&quot;</span><span class="token punctuation">,</span>
      <span class="token string">&quot;mockjs&quot;</span><span class="token operator">:</span><span class="token string">&quot;Mock&quot;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token doctype">&lt;!DOCTYPE html&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>en<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>utf-8<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">http-equiv</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>X-UA-Compatible<span class="token punctuation">&quot;</span></span> <span class="token attr-name">content</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>IE=edge<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>viewport<span class="token punctuation">&quot;</span></span> <span class="token attr-name">content</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>width=device-width,initial-scale=1.0<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>link</span> <span class="token attr-name">rel</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>icon<span class="token punctuation">&quot;</span></span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>&lt;%= BASE_URL %&gt;favicon.ico<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>link</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>https://cdn.bootcdn.net/ajax/libs/view-design/4.3.2/styles/iview.min.css<span class="token punctuation">&quot;</span></span> <span class="token attr-name">rel</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>stylesheet<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">&gt;</span></span>小羽呀~<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>noscript</span><span class="token punctuation">&gt;</span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>strong</span><span class="token punctuation">&gt;</span></span>We're sorry but hello-world doesn't work properly without JavaScript enabled. Please enable it to continue.<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>strong</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>noscript</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>app<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
    <span class="token comment">&lt;!-- built files will be auto injected --&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.min.js<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token script"></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>https://cdn.bootcdn.net/ajax/libs/axios/0.19.2/axios.min.js<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token script"></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>https://cdn.bootcdn.net/ajax/libs/vue-router/3.1.3/vue-router.min.js<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token script"></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>https://cdn.bootcdn.net/ajax/libs/vuex/3.2.0/vuex.min.js<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token script"></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>https://cdn.bootcdn.net/ajax/libs/echarts/4.6.0/echarts.common.min.js<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token script"></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>https://cdn.bootcdn.net/ajax/libs/view-design/4.3.2/iview.min.js<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token script"></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>https://cdn.bootcdn.net/ajax/libs/Mock.js/1.0.1-beta3/mock-min.js<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token script"></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>
    <span class="token comment">&lt;!-- &lt;% for (var i in htmlWebpackPlugin.options.cdn &amp;&amp; htmlWebpackPlugin.options.cdn.js) { %&gt;
      &lt;script src=&quot;&lt;%= htmlWebpackPlugin.options.cdn.js[i] %&gt;&quot;&gt;&lt;/script&gt;
    &lt;% } %&gt; --&gt;</span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br></div></div><h3 id="_2-4-图片压缩"><a href="#_2-4-图片压缩" class="header-anchor">#</a> 2.4 图片压缩</h3> <p>用Vue Cli3搭建的Vue项目中，图片是没进行压缩就直接用url-loader和file-loader处理。可以使用image-webpack-loader进行压缩图片。</p> <p>安装image-webpack-loader</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code>cnpm <span class="token function">install</span> image-webpack-loader --save-dev
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>vue.config.js中配置图片压缩</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token function-variable function">chainWebpack</span><span class="token operator">:</span> <span class="token parameter">config</span> <span class="token operator">=&gt;</span><span class="token punctuation">{</span>
        config<span class="token punctuation">.</span>module
            <span class="token punctuation">.</span><span class="token function">rule</span><span class="token punctuation">(</span><span class="token string">'images'</span><span class="token punctuation">)</span>
            <span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span><span class="token regex">/\.(png|jpe?g|gif|svg)(\?.*)?$/</span><span class="token punctuation">)</span>
            <span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span><span class="token string">'image-webpack-loader'</span><span class="token punctuation">)</span>
            <span class="token punctuation">.</span><span class="token function">loader</span><span class="token punctuation">(</span><span class="token string">'image-webpack-loader'</span><span class="token punctuation">)</span>
            <span class="token punctuation">.</span><span class="token function">options</span><span class="token punctuation">(</span><span class="token punctuation">{</span> bypassOnDebug<span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h2 id="_3-部署优化"><a href="#_3-部署优化" class="header-anchor">#</a> 3.部署优化</h2> <h3 id="_3-1-服务器开启压缩模块"><a href="#_3-1-服务器开启压缩模块" class="header-anchor">#</a> 3.1 服务器开启压缩模块</h3> <p>使用gzip压缩功能，可能为我们节约带宽，加快传输速度，有更好的体验，也为我们节约成本，所以说这是一个重点。</p> <p><code>Brotli</code>是<code>Google</code>推出的开源压缩算法，通过变种的<code>LZ77</code>算法、<code>Huffman</code>编码以及二阶文本建模等方式进行数据压缩，与其他压缩算法相比，它有着更高的压缩效率，性能也比我们目前常见的<code>Gzip</code>高 17-25%，可以帮我们更高效的压缩网页中的各类文件大小及脚本，从而提高加载速度，提升网页浏览体验。</p> <h4 id="_3-1-1-nginx开启动态gizp压缩"><a href="#_3-1-1-nginx开启动态gizp压缩" class="header-anchor">#</a> 3.1.1 nginx开启动态gizp压缩</h4> <div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code><span class="token keyword">gzip</span> on<span class="token punctuation">;</span>
<span class="token keyword">gzip_min_length</span> <span class="token number">2</span>k<span class="token punctuation">;</span>
<span class="token keyword">gzip_buffers</span>   <span class="token number">4</span> <span class="token number">32</span>k<span class="token punctuation">;</span>
<span class="token keyword">gzip_http_version</span> <span class="token number">1.1</span><span class="token punctuation">;</span>
<span class="token keyword">gzip_comp_level</span> <span class="token number">6</span><span class="token punctuation">;</span>
<span class="token keyword">gzip_types</span> text<span class="token operator">/</span>plain text<span class="token operator">/</span>css text<span class="token operator">/</span>javascript application<span class="token operator">/</span>json application<span class="token operator">/</span>javascript application<span class="token operator">/</span>x<span class="token operator">-</span>javascript application<span class="token operator">/</span>xml<span class="token punctuation">;</span>
<span class="token keyword">gzip_vary</span> on<span class="token punctuation">;</span>
<span class="token keyword">gzip_proxied</span> any<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h4 id="_3-1-2-nginx开启静态gzip压缩"><a href="#_3-1-2-nginx开启静态gzip压缩" class="header-anchor">#</a> 3.1.2 nginx开启静态gzip压缩</h4> <p>需要安装ngx_http_gzip_static_module模块</p> <ul><li>gzip_static配置优先级高于gzip</li> <li>开启nginx_static后，对于任何文件都会先查找是否有对应的gz文件</li> <li>gzip_types设置对gzip_static无效</li></ul> <div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code><span class="token keyword">gzip_static</span> on<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="_3-1-3-nginx开启brotli压缩"><a href="#_3-1-3-nginx开启brotli压缩" class="header-anchor">#</a> 3.1.3 nginx开启brotli压缩</h4> <p>需要安装ngx_brotli模块</p> <div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code><span class="token comment">#br压缩</span>
brotli on<span class="token punctuation">;</span>              <span class="token comment">#启用</span>
brotli_comp_level <span class="token number">6</span><span class="token punctuation">;</span>    <span class="token comment">#等级，默认 6，最高 11，太高的压缩水平可能需要更多的 CPU</span>
brotli_buffers <span class="token number">16</span> <span class="token number">32</span>k<span class="token punctuation">;</span>   <span class="token comment">#请求缓冲区的数量和大小</span>
brotli_min_length <span class="token number">128</span><span class="token punctuation">;</span>   <span class="token comment">#指定压缩数据的最小长度，只有大于或等于最小长度才会对其压缩。这里指定 128 字节</span>
brotli_types text<span class="token operator">/</span>plain application<span class="token operator">/</span>javascript application<span class="token operator">/</span>x<span class="token operator">-</span>javascript text<span class="token operator">/</span>javascript text<span class="token operator">/</span>css application<span class="token operator">/</span>xml application<span class="token operator">/</span>json image<span class="token operator">/</span>svg application<span class="token operator">/</span>font<span class="token operator">-</span>woff application<span class="token operator">/</span>vnd<span class="token punctuation">.</span>ms<span class="token operator">-</span>fontobject application<span class="token operator">/</span>vnd<span class="token punctuation">.</span>apple<span class="token punctuation">.</span>mpegurl <span class="token punctuation">;</span>   <span class="token comment">#指定允许进行压缩类型</span>
brotli_static always<span class="token punctuation">;</span>   <span class="token comment">#是否允许查找预处理好的、以.br 结尾的压缩文件，可选值为 on、off、always</span>
brotli_window <span class="token number">512</span>k<span class="token punctuation">;</span>     <span class="token comment">#窗口值，默认值为 512k</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="_3-2-http2-0"><a href="#_3-2-http2-0" class="header-anchor">#</a> 3.2 http2.0</h3> <h4 id="_3-2-1-二进制分帧"><a href="#_3-2-1-二进制分帧" class="header-anchor">#</a> 3.2.1 二进制分帧</h4> <p><code>HTTP2.0</code>通过在应用层和传输层之间增加一个二进制分帧层，突破了<code>HTTP1.1</code>的性能限制、改进传输性能。</p> <p><img src="/assets/img/293521278-5a6e83af997f9_articlex.101ddc94.jpg" alt="293521278-5a6e83af997f9_articlex"></p> <p>可见，虽然<code>HTTP2.0</code>的协议和<code>HTTP1.x</code>协议之间的规范完全不同了，但是实际上<code>HTTP2.0</code>并没有改变<code>HTTP1.x</code>的语义。
简单来说，<code>HTTP2.0</code>只是把原来<code>HTTP1.x</code>的<code>header</code>和<code>body</code>部分用<code>frame</code>重新封装了一层而已。</p> <h4 id="_3-2-2-多路复用"><a href="#_3-2-2-多路复用" class="header-anchor">#</a> 3.2.2 多路复用</h4> <p>下面是几个概念：</p> <ul><li>流（<code>stream</code>）：已建立连接上的双向字节流。</li> <li>消息：与逻辑消息对应的完整的一系列数据帧。</li> <li>帧（<code>frame</code>）：<code>HTTP2.0</code>通信的最小单位，每个帧包含帧头部，至少也会标识出当前帧所属的流（<code>stream id</code>）。</li></ul> <p><img src="/assets/img/928968545-5a6e83afcf289_articlex.ff4ca77b.jpg" alt="928968545-5a6e83afcf289_articlex"></p> <p>从图中可见，所有的<code>HTTP2.0</code>通信都在一个<code>TCP</code>连接上完成，这个连接可以承载任意数量的双向数据流。</p> <p>每个数据流以消息的形式发送，而消息由一或多个帧组成。这些帧可以乱序发送，然后再根据每个帧头部的流标识符（<code>stream id</code>）重新组装。</p> <p>举个例子，每个请求是一个数据流，数据流以消息的方式发送，而消息又分为多个帧，帧头部记录着<code>stream id</code>用来标识所属的数据流，不同属的帧可以在连接中随机混杂在一起。接收方可以根据<code>stream id</code>将帧再归属到各自不同的请求当中去。</p> <p>另外，多路复用（连接共享）可能会导致关键请求被阻塞。<code>HTTP2.0</code>里每个数据流都可以设置优先级和依赖，优先级高的数据流会被服务器优先处理和返回给客户端，数据流还可以依赖其他的子数据流。</p> <p>可见，<code>HTTP2.0</code>实现了真正的并行传输，它能够在一个<code>TCP</code>上进行任意数量<code>HTTP</code>请求。而这个强大的功能则是基于“二进制分帧”的特性。</p> <h4 id="_3-2-3-头部压缩"><a href="#_3-2-3-头部压缩" class="header-anchor">#</a> 3.2.3 头部压缩</h4> <p>在<code>HTTP1.x</code>中，头部元数据都是以纯文本的形式发送的，通常会给每个请求增加500~800字节的负荷。</p> <p>比如说<code>cookie</code>，默认情况下，浏览器会在每次请求的时候，把<code>cookie</code>附在<code>header</code>上面发送给服务器。（由于<code>cookie</code>比较大且每次都重复发送，一般不存储信息，只是用来做状态记录和身份认证）</p> <p><code>HTTP2.0</code>使用<code>encoder</code>来减少需要传输的<code>header</code>大小，通讯双方各自<code>cache</code>一份<code>header fields</code>表，既避免了重复<code>header</code>的传输，又减小了需要传输的大小。高效的压缩算法可以很大的压缩<code>header</code>，减少发送包的数量从而降低延迟。</p> <h4 id="_3-2-4-服务器推送"><a href="#_3-2-4-服务器推送" class="header-anchor">#</a> 3.2.4 服务器推送</h4> <p>服务器除了对最初请求的响应外，服务器还可以额外的向客户端推送资源，而无需客户端明确的请求。</p> <h4 id="_3-2-5-nginx开启http2-0"><a href="#_3-2-5-nginx开启http2-0" class="header-anchor">#</a> 3.2.5 nginx开启http2.0</h4> <ul><li>需要https协议</li> <li>需要开启http_v2_module模块</li></ul> <div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code><span class="token keyword">server</span> <span class="token punctuation">{</span>
        <span class="token keyword">listen</span>       <span class="token number">80</span><span class="token punctuation">;</span>
        <span class="token keyword">server_name</span>  www<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com<span class="token punctuation">;</span>
        <span class="token keyword">rewrite</span>     <span class="token operator">^</span>   <span class="token keyword">https</span><span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span><span class="token variable">$server_name</span><span class="token variable">$request_uri</span><span class="token operator">?</span> permanent<span class="token punctuation">;</span>

        <span class="token comment">#charset koi8-r;</span>

        <span class="token comment">#access_log  logs/host.access.log  main;</span>


     <span class="token punctuation">}</span>
     <span class="token keyword">server</span> <span class="token punctuation">{</span>
        <span class="token keyword">listen</span> <span class="token number">443</span> <span class="token keyword">ssl</span> http2 default_server<span class="token punctuation">;</span>
        <span class="token keyword">server_name</span> www<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com<span class="token punctuation">;</span>
        <span class="token keyword">ssl_certificate</span>  <span class="token operator">/</span>etc<span class="token operator">/</span>nginx<span class="token operator">/</span>home<span class="token operator">/</span>www<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com_ssl<span class="token operator">/</span>www<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com_bundle<span class="token punctuation">.</span>crt<span class="token punctuation">;</span>
        <span class="token keyword">ssl_certificate_key</span>  <span class="token operator">/</span>etc<span class="token operator">/</span>nginx<span class="token operator">/</span>home<span class="token operator">/</span>www<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com_ssl<span class="token operator">/</span>www<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com<span class="token punctuation">.</span>key<span class="token punctuation">;</span>
        <span class="token keyword">ssl_session_timeout</span> <span class="token number">5</span>m<span class="token punctuation">;</span>
        <span class="token comment">#请按照以下协议配置</span>
        <span class="token keyword">ssl_protocols</span> TLSv1 TLSv1<span class="token punctuation">.</span><span class="token number">1</span> TLSv1<span class="token punctuation">.</span><span class="token number">2</span><span class="token punctuation">;</span>
        <span class="token comment">#请按照以下套件配置，配置加密套件，写法遵循 openssl 标准。</span>
        <span class="token keyword">ssl_ciphers</span> ECDHE<span class="token operator">-</span>RSA<span class="token operator">-</span>AES128<span class="token operator">-</span>GCM<span class="token operator">-</span>SHA256<span class="token punctuation">:</span>HIGH<span class="token punctuation">:</span><span class="token operator">!</span>aNULL<span class="token punctuation">:</span><span class="token operator">!</span>MD5<span class="token punctuation">:</span><span class="token operator">!</span>RC4<span class="token punctuation">:</span><span class="token operator">!</span>DHE<span class="token punctuation">;</span>
        <span class="token keyword">ssl_prefer_server_ciphers</span> on<span class="token punctuation">;</span>
        <span class="token keyword">location</span> <span class="token operator">/</span> <span class="token punctuation">{</span>
            <span class="token keyword">proxy_redirect</span> off<span class="token punctuation">;</span>
            <span class="token keyword">proxy_set_header</span> Host <span class="token variable">$host</span><span class="token punctuation">;</span>
            <span class="token keyword">proxy_set_header</span> X<span class="token operator">-</span>Real<span class="token operator">-</span>IP <span class="token variable">$remote_addr</span><span class="token punctuation">;</span>
            <span class="token keyword">proxy_set_header</span> X<span class="token operator">-</span>Forwarded<span class="token operator">-</span>For <span class="token variable">$proxy_add_x_forwarded_for</span><span class="token punctuation">;</span>
            <span class="token keyword">proxy_pass</span> <span class="token keyword">http</span><span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span><span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token punctuation">:</span><span class="token number">8000</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">location</span> <span class="token operator">=</span> <span class="token operator">/</span><span class="token number">50</span>x<span class="token punctuation">.</span>html <span class="token punctuation">{</span>
            <span class="token keyword">root</span>   html<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br></div></div><h3 id="_3-3-提高浏览器的并发链接数"><a href="#_3-3-提高浏览器的并发链接数" class="header-anchor">#</a> 3.3 提高浏览器的并发链接数</h3> <p>不同的浏览器对单个域名的最大并发连接数有一定的限制，HTTP/1.0和HTTP/1.1也不相同。比如HTTP/1.1协议下，IE6的并发连接数限制是2个；而在HTTP/1.0下，IE6的并发连接数可以达到4个。在其它浏览器也有类似的限制，一般是4~8个。这个时候，如果浏览器同时对某一域名发起多个请求，超过了限制就会出现等待，也就是阻挡。</p> <p>那么为了解决阻挡这一问题，我们可以对某些URL的域名分散处理。比如我们的图片域名，一般用类似img.test.com的域名，当一个页面包含20多张图片的时候，那至少有10几个请求会被阻挡，而如果我们分散到img0.test.com/img1.test.com/img2.test.com/…等不同域名的时候，至少这20个图片请求会并发进行，网站打开速度会明显提升很多。类似的，可以对一些css/js的域名同样处理。</p> <h3 id="_3-4-开启http缓存"><a href="#_3-4-开启http缓存" class="header-anchor">#</a> 3.4 开启http缓存</h3> <p>以下为nginx的配置</p> <h4 id="_3-4-1-expires"><a href="#_3-4-1-expires" class="header-anchor">#</a> 3.4.1 expires</h4> <div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code><span class="token keyword">location</span> <span class="token operator">~</span> <span class="token punctuation">.</span><span class="token operator">*</span>\<span class="token punctuation">.</span><span class="token punctuation">(</span>gif<span class="token operator">|</span>ico<span class="token operator">|</span>jpe<span class="token operator">?</span>g<span class="token operator">|</span>png<span class="token operator">|</span>bmp<span class="token operator">|</span>swf<span class="token operator">|</span>js<span class="token operator">|</span>css<span class="token punctuation">)</span>$ <span class="token punctuation">{</span>
    <span class="token keyword">expires</span> <span class="token number">24</span>h<span class="token punctuation">;</span>
    <span class="token keyword">root</span>  <span class="token operator">/</span>home<span class="token operator">/</span>www<span class="token operator">/</span>test<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h4 id="_3-4-2-cache-control"><a href="#_3-4-2-cache-control" class="header-anchor">#</a> 3.4.2 Cache-control</h4> <div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code><span class="token keyword">location</span> <span class="token operator">~</span> <span class="token punctuation">.</span><span class="token operator">*</span>\<span class="token punctuation">.</span><span class="token punctuation">(</span>gif<span class="token operator">|</span>ico<span class="token operator">|</span>jpe<span class="token operator">?</span>g<span class="token operator">|</span>png<span class="token operator">|</span>bmp<span class="token operator">|</span>swf<span class="token operator">|</span>js<span class="token operator">|</span>css<span class="token punctuation">)</span>$ <span class="token punctuation">{</span>
    <span class="token comment">#设置过期时间为3600s（1h）</span>
    <span class="token keyword">add_header</span> Cache<span class="token operator">-</span>Control public<span class="token punctuation">,</span>max<span class="token operator">-</span>age<span class="token operator">=</span><span class="token number">3600</span><span class="token punctuation">;</span>
    <span class="token keyword">root</span>  <span class="token operator">/</span>opt<span class="token operator">/</span>app<span class="token operator">/</span>code<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></div> <footer class="page-edit" style="display:none;"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">2020-8-19 9:23:16</span></div></footer> <!----> <!----> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-40a3448a><li class="level-2" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_1-源码优化" class="sidebar-link" data-v-40a3448a>1.源码优化</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_1-1-v-for使用key" class="sidebar-link" data-v-40a3448a>1.1 v-for使用key</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_1-2-路由懒加载" class="sidebar-link" data-v-40a3448a>1.2 路由懒加载</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_1-3-及时销毁定时器" class="sidebar-link" data-v-40a3448a>1.3 及时销毁定时器</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_1-4-合理利用生命周期" class="sidebar-link" data-v-40a3448a>1.4 合理利用生命周期</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_1-5-利用v-if和v-show减少初始化渲染和切换渲染的性能开销" class="sidebar-link" data-v-40a3448a>1.5 利用v-if和v-show减少初始化渲染和切换渲染的性能开销</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_1-6-computed、watch、methods区分使用场景" class="sidebar-link" data-v-40a3448a>1.6 computed、watch、methods区分使用场景</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_1-7-利用v-once处理只会渲染一次的元素或组件" class="sidebar-link" data-v-40a3448a>1.7 利用v-once处理只会渲染一次的元素或组件</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_1-8-长列表性能优化（object-freeze）" class="sidebar-link" data-v-40a3448a>1.8 长列表性能优化（Object.freeze）</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_1-9-提前过滤掉非必须数据，优化data选项中的数据结构" class="sidebar-link" data-v-40a3448a>1.9 提前过滤掉非必须数据，优化data选项中的数据结构</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_1-10-避免在v-for循环中读取data中数组类型的数据" class="sidebar-link" data-v-40a3448a>1.10 避免在v-for循环中读取data中数组类型的数据</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_1-11-防抖和节流" class="sidebar-link" data-v-40a3448a>1.11 防抖和节流</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_1-12-图片懒加载" class="sidebar-link" data-v-40a3448a>1.12 图片懒加载</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_1-13-组件库的按需引入" class="sidebar-link" data-v-40a3448a>1.13 组件库的按需引入</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_1-14-dns预解析" class="sidebar-link" data-v-40a3448a>1.14 DNS预解析</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_1-15-无限列表的优化" class="sidebar-link" data-v-40a3448a>1.15 无限列表的优化</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_1-16-服务端渲染-ssr-or-预渲染" class="sidebar-link" data-v-40a3448a>1.16 服务端渲染 SSR or 预渲染</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_2-打包优化" class="sidebar-link" data-v-40a3448a>2.打包优化</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_2-1-生产环境不生成-map文件" class="sidebar-link" data-v-40a3448a>2.1 生产环境不生成.map文件</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_2-2-vue开启gzip压缩" class="sidebar-link" data-v-40a3448a>2.2 vue开启gzip压缩</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_2-3-cdn优化" class="sidebar-link" data-v-40a3448a>2.3 cdn优化</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_2-4-图片压缩" class="sidebar-link" data-v-40a3448a>2.4 图片压缩</a></li><li class="level-2" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_3-部署优化" class="sidebar-link" data-v-40a3448a>3.部署优化</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_3-1-服务器开启压缩模块" class="sidebar-link" data-v-40a3448a>3.1 服务器开启压缩模块</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_3-2-http2-0" class="sidebar-link" data-v-40a3448a>3.2 http2.0</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_3-3-提高浏览器的并发链接数" class="sidebar-link" data-v-40a3448a>3.3 提高浏览器的并发链接数</a></li><li class="level-3" data-v-40a3448a><a href="/blogs/%E5%89%8D%E7%AB%AF/2020/vue%E7%9A%84%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96.html#_3-4-开启http缓存" class="sidebar-link" data-v-40a3448a>3.4 开启http缓存</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-44bd5a18 data-v-44bd5a18><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-44bd5a18><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-44bd5a18></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-44bd5a18></path></svg></div></div></div>
    <script src="/assets/js/app.14e3b6f2.js" defer></script><script src="/assets/js/3.38ef4e4c.js" defer></script><script src="/assets/js/1.dd4aade8.js" defer></script><script src="/assets/js/11.dbb0142d.js" defer></script>
  </body>
</html>
